Data Wrangling
dataurl <- "https://raw.githubusercontent.com/ColeWCU/sta553/main/data/income_per_person.csv"
b <- tempfile()
download.file(dataurl,b,mode='wb')
income <- read.csv(b)
dataurl <- "https://raw.githubusercontent.com/ColeWCU/sta553/main/data/countries_total.csv"
b <- tempfile()
download.file(dataurl,b,mode='wb')
countries <- read.csv(b)
dataurl <- "https://raw.githubusercontent.com/ColeWCU/sta553/main/data/life_expectancy_years.csv"
b <- tempfile()
download.file(dataurl,b,mode='wb')
life <- read.csv(b)
dataurl <- "https://raw.githubusercontent.com/ColeWCU/sta553/main/data/population_total.csv"
b <- tempfile()
download.file(dataurl,b,mode='wb')
population <- read.csv(b)
dataurl <- "https://raw.githubusercontent.com/ColeWCU/sta553/main/data/POC.csv"
b <- tempfile()
download.file(dataurl,b,mode='wb')
gas <- read.csv(b)
income <- income %>% rename(country = geo) %>%
pivot_longer(cols = X1800:X2018) %>%
mutate(name = substr(name,2,5)) %>%
rename(year = name, income = value)
life <- life %>% rename(country = geo) %>%
pivot_longer(cols = X1800:X2018) %>%
mutate(name = substr(name,2,5)) %>%
rename(year = name, lifeExp = value)
LifeExpIncom <- income %>% left_join(life,by = c('country','year'))
population <- population%>%
pivot_longer(cols = X1800:X2018) %>%
mutate(name = substr(name,2,5)) %>%
rename(year = name, population = value)
LifeExpIncomregion <- LifeExpIncom %>% inner_join((countries %>% select(region,name)),by = c('country'='name')) %>% left_join((population)) %>%
mutate(year = as.integer(year),income = as.numeric(income),lifeExp=as.numeric(lifeExp),population = as.numeric(population))
head(LifeExpIncomregion,5)
## # A tibble: 5 x 7
## country year income lifeExp region geo population
## <chr> <int> <dbl> <dbl> <chr> <chr> <dbl>
## 1 Afghanistan 1800 603 28.2 Asia Afghanistan 3280000
## 2 Afghanistan 1800 603 28.2 Asia Albania 410000
## 3 Afghanistan 1800 603 28.2 Asia Algeria 2500000
## 4 Afghanistan 1800 603 28.2 Asia Andorra 2650
## 5 Afghanistan 1800 603 28.2 Asia Angola 1570000
Life Expectancy and Income in 2015 Plotly
z= (LifeExpIncomregion%>% filter(year == 2015))$country
plot_ly(
data = (LifeExpIncomregion %>% filter(year == 2015)# %>% mutate(population2 = population/max(population))
),
x = ~income, # Horizontal axis
y = ~lifeExp, # Vertical axis
color =z, #~factor(sort(rep(c(4,6,8,10,12,14,16), 50))), # must be a numeric factor
text = ~paste('Country: ',country,'\n','Population: ',population),
alpha = 0.1,
size = ~population,
type = "scatter",
mode = "markers"
) %>%
layout(showlegend = FALSE,
title = 'Income Vs. Life Expectancy',xaxis = list(title = 'Income'),
yaxis = list(title = 'Life Expectancy'))
# plot <- ggplot(LifeExpIncomregion %>% filter(year == 2015) %>%
# rename(`Life Expectancy` = lifeExp,Income =income,Country=country,Population=population))+
# geom_point(aes(x = Income,y = `Life Expectancy`,color=Country,size=Population),alpha=0.5)+
# labs(
# x = "Income",
# y = "Life Expectancy",
# title = "Income Vs. Life Expectancy") +
# theme(legend.position="none" )
# ggplotly(plot)
Change in Relationship between the Life Expectancy and Income gganimate
library(gapminder)
p <- ggplot(LifeExpIncomregion %>%
#filter(year>2000) %>%
filter(country %in% head(unique(country),25)) #To make the plot assemble faster
,aes(x = income,y = lifeExp,size=population,color=region
,alpha=0.5)) +
geom_point()+
labs(title = 'Year: {frame_time}',
x = 'Income',
y = 'life expectancy') +
transition_time(year)
animate(p, renderer = gifski_renderer())

Map
gas_data <- sample_n(gas,500)
poc <- read_csv("https://projectdat.s3.amazonaws.com/POC.csv")[,c(7,8,9, 17)]
poc.site <- poc[poc$POC == 1,]
geostyle <- list(scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = toRGB("gray95"),
subunitcolor = toRGB("gray85"),
countrycolor = toRGB("gray85"),
countrywidth = 0.5,
subunitwidth = 0.5
)
fig <- plot_geo(gas_data, lat = ~ycoord, lon = ~xcoord) %>%
add_markers(text = ~paste('state: ', STATE, '\n' , 'County: ', county, '\n', 'Address: ' , ADDRESS, '\n', 'Zip: ' , X),
color = "red",
symbol = I("circle"),
size = I(8),
hoverinfo = "text" ) %>%
layout( title = 'Gas Stations', geo = geostyle)
fig